package com.erp.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.erp.common.response.ListVo;
import com.erp.common.utils.BeanUtils;
import com.erp.user.entity.RolePermit;
import com.erp.user.mapper.RolePermitMapper;
import com.erp.user.request.*;
import com.erp.user.response.RolePermitDetailInfoResp;
import com.erp.user.response.RolePermitInfoResp;
import com.erp.user.response.RolePermitSearchInfoResp;
import com.erp.user.service.RolePermitService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;


/**
 * @author shensong
 * @program machining_system.v1.0.0
 * @description  服务实现类
 * @date 2022/12/21 10:56:35
 */
@Service("rolePermitService")
public class RolePermitServiceImpl extends ServiceImpl<RolePermitMapper, RolePermit> implements RolePermitService {

    @Override
    public ListVo<List<RolePermitInfoResp>> index(RolePermitPageReq request) {
        LambdaQueryWrapper<RolePermit> queryWrapper = Wrappers.lambdaQuery();
        if (Objects.nonNull(request)) {
            queryWrapper.eq(Objects.nonNull(request.getId()),RolePermit::getId, request.getId());
            queryWrapper.eq(Objects.nonNull(request.getPermitId()),RolePermit::getPermitId, request.getPermitId());
            queryWrapper.eq(Objects.nonNull(request.getRoleId()),RolePermit::getRoleId, request.getRoleId());
            queryWrapper.eq(Objects.nonNull(request.getGmtCreate()),RolePermit::getGmtCreate, request.getGmtCreate());
            queryWrapper.eq(Objects.nonNull(request.getGmtModified()),RolePermit::getGmtModified, request.getGmtModified());
        }
        queryWrapper.orderByDesc(RolePermit::getId);
        IPage<RolePermit> data = this.queryPage(request.getPageNum(), request.getPageSize(), queryWrapper);
        if (Objects.nonNull(data) && CollectionUtils.isNotEmpty(data.getRecords())) {
            List<RolePermitInfoResp> rolePermitListResponse = BeanUtils.copyObjects(data.getRecords(), RolePermitInfoResp.class);
            return new ListVo<>(rolePermitListResponse, data.getTotal(), data.getSize(), data.getCurrent());
        }

        return new ListVo<>();
    }

    @Override
    public Boolean store(RolePermitAddReq request) {
        RolePermit entity = BeanUtils.copyProperties(request, RolePermit.class);
        return this.save(entity);
    }

    @Override
    public Boolean update(RolePermitUpdateReq request) {
        RolePermit entity = BeanUtils.copyProperties(request, RolePermit.class);
        return this.updateById(entity);
    }

    @Override
    public Boolean destroy(RolePermitDeleteReq request) {
        return this.removeById(request.getPermitId());
    }

    @Override
    public RolePermitInfoResp show(RolePermitInfoReq request) {
        RolePermit entity = this.getById(request.getPermitId());
        return Objects.nonNull(entity) ? BeanUtils.copyProperties(entity, RolePermitInfoResp.class) : null;
    }

    @Override
    public Boolean approve(RolePermitApproveReq request) {
        RolePermit entity = BeanUtils.copyProperties(request, RolePermit.class);
        return this.updateById(entity);
    }

    @Override
    public Boolean enabled(RolePermitStatusReq request) {
        RolePermit entity = BeanUtils.copyProperties(request, RolePermit.class);
        return this.updateById(entity);
    }

    @Override
    public List<RolePermitInfoResp> option(RolePermitListReq request) {
        LambdaQueryWrapper<RolePermit> queryWrapper = Wrappers.lambdaQuery();
        if (Objects.nonNull(request)) {
            queryWrapper.eq(Objects.nonNull(request.getId()), RolePermit::getId, request.getId());
            queryWrapper.eq(Objects.nonNull(request.getPermitId()), RolePermit::getPermitId, request.getPermitId());
            queryWrapper.eq(Objects.nonNull(request.getRoleId()), RolePermit::getRoleId, request.getRoleId());
            queryWrapper.eq(Objects.nonNull(request.getGmtCreate()), RolePermit::getGmtCreate, request.getGmtCreate());
            queryWrapper.eq(Objects.nonNull(request.getGmtModified()), RolePermit::getGmtModified, request.getGmtModified());
        }

        queryWrapper.orderByDesc(RolePermit::getId);
        List<RolePermit> data = this.list(queryWrapper);
        return CollectionUtils.isNotEmpty(data) ? BeanUtils.copyObjects(data, RolePermitInfoResp.class) : Lists.newArrayList();
    }

    @Override
    public List<RolePermitSearchInfoResp> search(RolePermitSearchReq request) {
        LambdaQueryWrapper<RolePermit> queryWrapper = Wrappers.lambdaQuery();
        if (Objects.nonNull(request)) {
                queryWrapper.eq(Objects.nonNull(request.getId()), RolePermit::getId, request.getId());
                queryWrapper.eq(Objects.nonNull(request.getPermitId()), RolePermit::getPermitId, request.getPermitId());
                queryWrapper.eq(Objects.nonNull(request.getRoleId()), RolePermit::getRoleId, request.getRoleId());
                queryWrapper.eq(Objects.nonNull(request.getGmtCreate()), RolePermit::getGmtCreate, request.getGmtCreate());
                queryWrapper.eq(Objects.nonNull(request.getGmtModified()), RolePermit::getGmtModified, request.getGmtModified());
        }

        queryWrapper.orderByDesc(RolePermit::getId);
        List<RolePermit> data = this.list(queryWrapper);
        return CollectionUtils.isNotEmpty(data) ? BeanUtils.copyObjects(data, RolePermitSearchInfoResp.class) : Lists.newArrayList();
    }

    @Override
    public RolePermitDetailInfoResp detail(RolePermitInfoReq request) {
        return BeanUtils.copyProperties(this.show(request), RolePermitDetailInfoResp.class);
    }

    private IPage<RolePermit> queryPage(Long pageNum, Long pageSize, Wrapper<RolePermit> queryWrapper) {
        IPage<RolePermit> selectPage = new Page<>();
        selectPage.setCurrent(pageNum);
        selectPage.setSize(pageSize);

        IPage<RolePermit> page = this.baseMapper.selectPage(selectPage, queryWrapper);
        return page;
    }
}
